 package scripting.event;


import client.MapleCharacter;
import client.MapleQuestStatus;
import client.MapleTraitType;
import client.skills.SkillFactory;
import handling.channel.ChannelServer;
import handling.world.WorldBroadcastService;
import handling.world.WorldPartyService;
import handling.world.party.MapleParty;
import handling.world.party.MaplePartyCharacter;
import handling.world.party.PartySearch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MapleItemInformationProvider;
import server.Timer;
import server.life.MapleMonster;
import server.maps.MapleMap;
import server.maps.MapleMapFactory;
import server.quest.MapleQuest;
import server.squad.MapleSquad;
import tools.MaplePacketCreator;
import tools.Pair;
import tools.packet.MobPacket;
import tools.packet.UIPacket;

import javax.script.ScriptException;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
































 public class EventInstanceManager {
/*   35 */   private static final Logger log = LogManager.getLogger(EventInstanceManager.class);
/*   36 */   private final ReentrantReadWriteLock mutex = new ReentrantReadWriteLock();
/*   37 */   private final Lock rL = this.mutex.readLock(); private final Lock wL = this.mutex.writeLock();
/*   38 */   private final Map<String, String> infos = new HashMap<>();
   private final EventManager em;
   private final int channel;
   private final String name;
/*   42 */   private List<MapleCharacter> chars = new LinkedList<>();
/*   43 */   private List<Integer> dced = new LinkedList<>();
/*   44 */   private List<MapleMonster> mobs = new LinkedList<>();
/*   45 */   private Map<Integer, Integer> killCount = new HashMap<>();
/*   46 */   private Properties props = new Properties();
/*   47 */   private long timeStarted = 0L;
/*   48 */   private long eventTime = 0L;
/*   49 */   private List<Integer> mapIds = new LinkedList<>();
/*   50 */   private List<Boolean> isInstanced = new LinkedList<>();
   private ScheduledFuture<?> eventTimer;
   private boolean disposed = false;
/*   53 */   private int remainingTime = -1;
/*   54 */   private long remainingTimeStart = 0L;


   private int timeType;


   private int eventType;


   public EventInstanceManager(EventManager em, String name, int channel) {
/*   64 */     this.em = em;
/*   65 */     this.name = name;
/*   66 */     this.channel = channel;
   }




   public void registerPlayer(MapleCharacter chr) {
/*   73 */     if (this.disposed || chr == null) {
       return;
     }
     try {
/*   77 */       this.wL.lock();
       try {
/*   79 */         this.chars.add(chr);
       } finally {
/*   81 */         this.wL.unlock();
       }
/*   83 */       chr.setEventInstance(this);
/*   84 */       this.em.getIv().invokeFunction("playerEntry", new Object[] { this, chr });
/*   85 */     } catch (ScriptException ex) {
/*   86 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : playerEntry:\r\n" + ex);
/*   87 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }







   public void changedMap(MapleCharacter chr, int mapid) {
/*   97 */     if (this.disposed) {
       return;
     }
     try {
/*  101 */       this.em.getIv().invokeFunction("changedMap", new Object[] { this, chr, Integer.valueOf(mapid) });
/*  102 */     } catch (ScriptException ex) {
/*  103 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : changedMap:\r\n" + ex);
/*  104 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }







   public void timeOut(long delay, EventInstanceManager eim) {
                if (disposed || eim == null) {
                    return;
                }
                eventTimer = Timer.EventTimer.getInstance().schedule(() -> {
                    if (disposed || em == null) {
                        return;
                    }
                    try {
                        em.getIv().invokeFunction("scheduledTimeout", eim);
                    } catch (ScriptException ex) {
                        log.error("Event name: " + em.getName() + ".js, Instance name : " + name + ", method Name : scheduledTimeout:\r\n" + ex);
                    } catch (NoSuchMethodException ex) {
                        // Ignore
                    }
                }, delay);
   }




   public void showEventTimer(MapleCharacter player, int time) {
/*  133 */     switch (this.eventType) {
       case 3:
/*  135 */         player.getClient().announce(MaplePacketCreator.getClockTime1(1, time));

       case 2:
/*  138 */         if (this.timeType == 1) {
/*  139 */           player.getClient().announce(MaplePacketCreator.getClock(this.remainingTime / 1000));
           return;
         }
         return;

       case 8:
/*  145 */         player.getClient().announce(MaplePacketCreator.getClockTime3((int)(this.eventTime - getTimeLeft()) / 1000, (int)this.eventTime / 1000));
         break;
     }
/*  148 */     if (this.remainingTime > 0) {
/*  149 */       player.getClient().announce(MaplePacketCreator.getClockTime4((int)getRemainingTime(), this.remainingTime));
     }
/*  151 */     player.getClient().announce(MaplePacketCreator.getClock(time));
   }




   public long getRemainingTime() {
/*  158 */     return this.remainingTime - System.currentTimeMillis() - this.remainingTimeStart;
   }

   public void stopEventTimer() {
/*  162 */     this.eventTime = 0L;
/*  163 */     this.timeStarted = 0L;
/*  164 */     if (this.eventTimer != null) {
/*  165 */       this.eventTimer.cancel(false);
     }
   }




   public void restartEventTimer(long time) {
     try {
/*  174 */       if (this.disposed) {
         return;
       }
/*  177 */       this.timeStarted = System.currentTimeMillis();
/*  178 */       this.eventTime = time;
/*  179 */       if (this.eventTimer != null) {
/*  180 */         this.eventTimer.cancel(false);
       }
/*  182 */       this.eventTimer = null;
/*  183 */       int timesend = (int)time / 1000;
/*  184 */       for (MapleCharacter chr : getPlayers()) {
/*  185 */         chr.send(MaplePacketCreator.getClock(timesend));
       }
/*  187 */       timeOut(time, this);
/*  188 */     } catch (Exception ex) {
/*  189 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : restartEventTimer:\r\n" + ex);
     }
   }




   public void startEventTimer(long time) {
/*  197 */     restartEventTimer(time);
   }

   public void startEventTimer(int eventType, int timeType, long eventTime) {
/*  201 */     if (this.disposed) {
       return;
     }
/*  204 */     this.remainingTime = -1;
/*  205 */     this.remainingTimeStart = -1L;
/*  206 */     this.timeStarted = System.currentTimeMillis();
/*  207 */     this.eventTime = eventTime;
/*  208 */     this.timeType = timeType;
/*  209 */     this.eventType = eventType;
/*  210 */     if (this.eventTimer != null) {
/*  211 */       this.eventTimer.cancel(false);
/*  212 */       this.eventTimer = null;
     }
/*  214 */     eventType = (int)eventTime / 1000;
/*  215 */     for (MapleCharacter player : getPlayers()) {
/*  216 */       showEventTimer(player, eventType);
     }
/*  218 */     timeOut(eventTime);
   }

   public void timeOut(long n) {
/*  222 */     if (this.disposed) {
       return;
     }
/*  225 */     this.eventTimer = Timer.EventTimer.getInstance().schedule(() -> {


           if (!this.disposed && this.em != null) {

             try {
               this.em.getIv().invokeFunction("scheduledTimeout", new Object[] { this });
/*  232 */             } catch (ScriptException|NoSuchMethodException ex) {
               log.error("[Event Script] " + this.em + " Instance:" + this.name + " Method:scheduledTimeout Error!", ex);
             }
           }
         },n);
   }

   public void setRemainingTimer(int remainingTime) {
/*  240 */     if (this.disposed) {
       return;
     }
/*  243 */     this.remainingTime = remainingTime;
/*  244 */     this.timeStarted = System.currentTimeMillis();
/*  245 */     if (this.eventTimer != null) {
/*  246 */       this.eventTimer.cancel(false);
/*  247 */       this.eventTimer = null;
     }
/*  249 */     for (MapleCharacter player : getPlayers()) {
/*  250 */       showEventTimer(player, remainingTime);
     }
/*  252 */     timeOut((getTimeLeft() > remainingTime) ? remainingTime : getTimeLeft());
   }




   public void startEventClock(long time) {
/*  259 */     if (this.disposed) {
       return;
     }
/*  262 */     int timesend = (int)time / 1000;
/*  263 */     for (MapleCharacter chr : getPlayers()) {
/*  264 */       chr.send(MaplePacketCreator.getClock(timesend));
     }
   }


   public void stopEventClock() {
/*  270 */     if (this.disposed) {
       return;
     }
/*  273 */     for (MapleCharacter chr : getPlayers()) {
/*  274 */       chr.send(MaplePacketCreator.stopClock());
     }
   }




   public boolean isTimerStarted() {
/*  282 */     return (this.eventTime > 0L && this.timeStarted > 0L);
   }




   public long getTimeLeft() {
/*  289 */     return this.eventTime - System.currentTimeMillis() - this.timeStarted;
   }





   public void registerParty(MapleParty party, MapleMap map) {
/*  297 */     registerParty(party, map, 0);
   }

   public void registerParty(MapleParty party, MapleMap map, int questID) {
/*  301 */     if (this.disposed) {
       return;
     }
/*  304 */     for (MaplePartyCharacter pc : party.getMembers()) {
/*  305 */       MapleCharacter player = map.getCharacterById(pc.getId());
/*  306 */       if (player != null && questID > 0) {
/*  307 */         player.getQuestNAdd(MapleQuest.getInstance(questID)).setCustomData(String.valueOf(System.currentTimeMillis()));
       }
/*  309 */       registerPlayer(map.getCharacterById(pc.getId()));
     }
/*  311 */     PartySearch ps = WorldPartyService.getInstance().getSearch(party);
/*  312 */     if (ps != null) {
/*  313 */       WorldPartyService.getInstance().removeSearch(ps, "开始组队任务，组队广告已被删除。");
     }
   }









   public void unregisterPlayer(MapleCharacter chr) {
/*  326 */     if (this.disposed) {
/*  327 */       chr.setEventInstance(null);
       return;
     }
/*  330 */     this.wL.lock();
     try {
/*  332 */       unregisterPlayer_NoLock(chr);
     } finally {
/*  334 */       this.wL.unlock();
     }
   }

   private boolean unregisterPlayer_NoLock(MapleCharacter chr) {
/*  339 */     if (this.name.equals("CWKPQ")) {
/*  340 */       MapleSquad squad = ChannelServer.getInstance(this.channel).getMapleSquad("CWKPQ");
/*  341 */       if (squad != null) {
/*  342 */         squad.removeMember(chr.getName());
/*  343 */         if (squad.getLeaderName().equals(chr.getName())) {
/*  344 */           this.em.setProperty("leader", "false");
         }
       }
     }
/*  348 */     chr.setEventInstance(null);
/*  349 */     if (this.disposed) {
/*  350 */       return false;
     }
/*  352 */     if (this.chars.contains(chr)) {
/*  353 */       this.chars.remove(chr);
/*  354 */       return true;
     }
/*  356 */     return false;
   }






   public boolean disposeIfPlayerBelow(byte size, int towarp) {
/*  365 */     if (this.disposed) {
/*  366 */       return true;
     }
/*  368 */     MapleMap map = null;
/*  369 */     if (towarp > 0) {
/*  370 */       map = getMapFactory().getMap(towarp);
     }

/*  373 */     this.wL.lock();
     try {
/*  375 */       if (this.chars != null && this.chars.size() <= size) {
/*  376 */         List<MapleCharacter> chrs = new LinkedList<>(this.chars);
/*  377 */         for (MapleCharacter chr : chrs) {
/*  378 */           if (chr == null) {
             continue;
           }
/*  381 */           unregisterPlayer_NoLock(chr);
/*  382 */           if (towarp > 0) {
/*  383 */             chr.changeMap(map, map.getPortal(0));
           }
         }
/*  386 */         dispose_NoLock();
/*  387 */         return true;
       }
/*  389 */     } catch (Exception ex) {
/*  390 */       log.error("", ex);
     } finally {
/*  392 */       this.wL.unlock();
     }
/*  394 */     return false;
   }









   public void saveBossQuest(int points) {
/*  406 */     if (this.disposed) {
       return;
     }
/*  409 */     for (MapleCharacter chr : getPlayers()) {
/*  410 */       MapleQuestStatus record = chr.getQuestNAdd(MapleQuest.getInstance(150001));
/*  411 */       if (record.getCustomData() != null) {
/*  412 */         record.setCustomData(String.valueOf(points + Integer.parseInt(record.getCustomData())));
       } else {
/*  414 */         record.setCustomData(String.valueOf(points));
       }
/*  416 */       chr.modifyCSPoints(2, points / 5, true);
/*  417 */       chr.getTrait(MapleTraitType.will).addExp(points / 100, chr);
     }
   }









   public void saveNX(int points) {
/*  430 */     if (this.disposed) {
       return;
     }
/*  433 */     for (MapleCharacter chr : getPlayers()) {
/*  434 */       chr.modifyCSPoints(2, points, true);
     }
   }









   public List<MapleCharacter> getPlayers() {
/*  447 */     if (this.disposed) {
/*  448 */       return Collections.emptyList();
     }
/*  450 */     this.rL.lock();
     try {
/*  452 */       return new LinkedList<>(this.chars);
     } finally {
/*  454 */       this.rL.unlock();
     }
   }




   public List<Integer> getDisconnected() {
/*  462 */     return this.dced;
   }









   public int getPlayerCount() {
/*  474 */     if (this.disposed) {
/*  475 */       return 0;
     }
/*  477 */     return this.chars.size();
   }









   public void registerMonster(MapleMonster mob) {
/*  489 */     if (this.disposed) {
       return;
     }
/*  492 */     this.mobs.add(mob);
/*  493 */     mob.setEventInstance(this);
   }





   public void unregisterMonster(MapleMonster mob) {
/*  501 */     mob.setEventInstance(null);
/*  502 */     if (this.disposed) {
       return;
     }
/*  505 */     if (this.mobs.contains(mob)) {
/*  506 */       this.mobs.remove(mob);
     }
/*  508 */     if (this.mobs.isEmpty()) {
       try {
/*  510 */         this.em.getIv().invokeFunction("allMonstersDead", new Object[] { this });
/*  511 */       } catch (ScriptException ex) {
/*  512 */         log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : allMonstersDead:\r\n" + ex);
/*  513 */       } catch (NoSuchMethodException noSuchMethodException) {}
     }
   }








   public void playerKilled(MapleCharacter chr) {
/*  525 */     if (this.disposed) {
       return;
     }
     try {
/*  529 */       this.em.getIv().invokeFunction("playerDead", new Object[] { this, chr });
/*  530 */     } catch (ScriptException ex) {
/*  531 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : playerDead:\r\n" + ex);
/*  532 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }








   public boolean revivePlayer(MapleCharacter chr) {
/*  543 */     if (this.disposed) {
/*  544 */       return false;
     }
     try {
/*  547 */       Object b = this.em.getIv().invokeFunction("playerRevive", new Object[] { this, chr });
/*  548 */       if (b instanceof Boolean) {
/*  549 */         return ((Boolean)b).booleanValue();
       }
/*  551 */     } catch (ScriptException ex) {
/*  552 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : playerRevive:\r\n" + ex);
/*  553 */     } catch (NoSuchMethodException noSuchMethodException) {}


/*  556 */     return true;
   }






   public void playerDisconnected(MapleCharacter chr, int idz) {
     byte ret;
/*  566 */     if (this.disposed) {
       return;
     }

     try {
/*  571 */       ret = ((Double)this.em.getIv().invokeFunction("playerDisconnected", new Object[] { this, chr })).byteValue();
/*  572 */     } catch (Exception e) {
/*  573 */       ret = 0;
     }

/*  576 */     this.wL.lock();
     try {
/*  578 */       if (this.disposed) {
         return;
       }
/*  581 */       if (chr == null || chr.isAlive()) {
/*  582 */         this.dced.add(Integer.valueOf(idz));
       }
/*  584 */       if (chr != null) {
/*  585 */         unregisterPlayer_NoLock(chr);
       }
/*  587 */       if (ret == 0) {
/*  588 */         if (getPlayerCount() <= 0) {
/*  589 */           dispose_NoLock();
         }
/*  591 */       } else if ((ret > 0 && getPlayerCount() < ret) || (ret < 0 && (isLeader(chr) || getPlayerCount() < ret * -1))) {
/*  592 */         List<MapleCharacter> chrs = new LinkedList<>(this.chars);
/*  593 */         for (MapleCharacter player : chrs) {
/*  594 */           if (player.getId() != idz) {
/*  595 */             removePlayer(player);
           }
         }
/*  598 */         dispose_NoLock();
       }
/*  600 */     } catch (Exception ex) {
/*  601 */       log.error("", ex);
     } finally {
/*  603 */       this.wL.unlock();
     }
   }








































   public void monsterKilled(MapleCharacter chr, MapleMonster mob) {
/*  647 */     if (this.disposed) {
       return;
     }
     try {
/*  651 */       int inc = ((Integer)this.em.getIv().invokeFunction("monsterValue", new Object[] { this, Integer.valueOf(mob.getId()) })).intValue();
/*  652 */       if (this.disposed || chr == null) {
         return;
       }
/*  655 */       Integer kc = this.killCount.get(Integer.valueOf(chr.getId()));
/*  656 */       if (kc == null) {
/*  657 */         kc = Integer.valueOf(inc);
       } else {
/*  659 */         kc = Integer.valueOf(kc.intValue() + inc);
       }
/*  661 */       this.killCount.put(Integer.valueOf(chr.getId()), kc);
/*  662 */     } catch (ScriptException ex) {
/*  663 */       log.error("Event name" + ((this.em == null) ? "null" : this.em.getName()) + ", Instance name : " + this.name + ", method Name : monsterValue:\r\n" + ex);
/*  664 */     } catch (NoSuchMethodException noSuchMethodException) {

/*  666 */     } catch (Exception ex) {
/*  667 */       log.error("", ex);
     }
     try {
/*  670 */       this.em.getIv().invokeFunction("monsterKilled", new Object[] { this, chr, Integer.valueOf(mob.getId()) });
/*  671 */     } catch (ScriptException ex2) {
/*  672 */       log.error("[Event Script] " + this.em.getName() + " Instance:" + this.name + " Method:monsterKilled Error!", ex2);
/*  673 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }













   public void monsterDamaged(MapleCharacter chr, MapleMonster mob, long damage) {
/*  689 */     if (this.disposed) {
       return;
     }
     try {
/*  693 */       this.em.getIv().invokeFunction("monsterDamaged", new Object[] { this, chr, Integer.valueOf(mob.getId()), Long.valueOf(damage) });
/*  694 */     } catch (ScriptException ex) {
/*  695 */       log.error("Event name: " + ((this.em == null) ? "null" : (this.em.getName() + ".js")) + ", Instance name : " + this.name + ", method Name : monsterValue:\r\n" + ex);
/*  696 */     } catch (NoSuchMethodException noSuchMethodException) {

/*  698 */     } catch (Exception ex) {
/*  699 */       log.error(ex);
     }
   }







   public void monsterDrop(MapleCharacter chr, MapleMonster mob) {
/*  710 */     if (this.disposed) {
       return;
     }
     try {
/*  714 */       this.em.getIv().invokeFunction("monsterDrop", new Object[] { this, chr, mob });
/*  715 */     } catch (ScriptException ex) {
/*  716 */       log.error("Event name: " + ((this.em == null) ? "null" : (this.em.getName() + ".js")) + ", Instance name : " + this.name + ", method Name : monsterDrop:\r\n" + ex);
/*  717 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }







   public void addPVPScore(MapleCharacter chr, int score) {
/*  727 */     if (this.disposed) {
       return;
     }
     try {
/*  731 */       this.em.getIv().invokeFunction("addPVPScore", new Object[] { this, chr, Integer.valueOf(score) });
/*  732 */     } catch (ScriptException ex) {
/*  733 */       log.error("Event name: " + ((this.em == null) ? "null" : (this.em.getName() + ".js")) + ", Instance name : " + this.name + ", method Name : monsterValue:\r\n" + ex);
/*  734 */     } catch (NoSuchMethodException noSuchMethodException) {

/*  736 */     } catch (Exception ex) {
/*  737 */       log.error(ex);
     }
   }










   public int getKillCount(MapleCharacter chr) {
/*  751 */     if (this.disposed) {
/*  752 */       return 0;
     }
/*  754 */     Integer kc = this.killCount.get(Integer.valueOf(chr.getId()));
/*  755 */     if (kc == null) {
/*  756 */       return 0;
     }
/*  758 */     return kc.intValue();
   }





   public void dispose_NoLock() {
/*  766 */     if (this.disposed || this.em == null) {
       return;
     }
/*  769 */     String emName = this.em.getName();
     try {
/*  771 */       this.disposed = true;
/*  772 */       for (MapleCharacter chr : this.chars) {
/*  773 */         chr.setEventInstance(null);
       }
/*  775 */       this.chars.clear();
/*  776 */       this.chars = null;
/*  777 */       if (this.mobs.size() >= 1) {
/*  778 */         for (MapleMonster mob : this.mobs) {
/*  779 */           if (mob != null) {
/*  780 */             mob.setEventInstance(null);
           }
         }
       }
/*  784 */       this.mobs.clear();
/*  785 */       this.mobs = null;
/*  786 */       this.killCount.clear();
/*  787 */       this.killCount = null;
/*  788 */       this.infos.clear();
/*  789 */       this.dced.clear();
/*  790 */       this.dced = null;
/*  791 */       this.timeStarted = 0L;
/*  792 */       this.eventTime = 0L;
/*  793 */       this.props.clear();
/*  794 */       this.props = null;
/*  795 */       for (int i = 0; i < this.mapIds.size(); i++) {
/*  796 */         if (((Boolean)this.isInstanced.get(i)).booleanValue()) {
/*  797 */           getMapFactory().removeInstanceMap(((Integer)this.mapIds.get(i)).intValue());
         }
       }
/*  800 */       this.mapIds.clear();
/*  801 */       this.mapIds = null;
/*  802 */       this.isInstanced.clear();
/*  803 */       this.isInstanced = null;
/*  804 */       this.em.disposeInstance(this.name);
/*  805 */     } catch (Exception e) {
/*  806 */       log.error("Caused by : " + emName + " instance name: " + this.name + " method: dispose \r\n" + e);
     }
   }


   public void dispose() {
/*  812 */     this.wL.lock();
     try {
/*  814 */       dispose_NoLock();
     } finally {
/*  816 */       this.wL.unlock();
     }
   }




   public ChannelServer getChannelServer() {
/*  824 */     return ChannelServer.getInstance(this.channel);
   }




   public List<MapleMonster> getMobs() {
/*  831 */     return this.mobs;
   }






   public void giveAchievement(int type) {
/*  840 */     if (this.disposed) {
       return;
     }
/*  843 */     for (MapleCharacter chr : getPlayers()) {
/*  844 */       chr.finishAchievement(type);
     }
   }






   public void broadcastPlayerMsg(int type, String msg) {
/*  854 */     if (this.disposed) {
       return;
     }
/*  857 */     for (MapleCharacter chr : getPlayers()) {
/*  858 */       chr.dropMessage(type, msg);
     }
   }






   public List<Pair<Integer, String>> newPair() {
/*  868 */     return new ArrayList<>();
   }






   public void addToPair(List<Pair<Integer, String>> e, int e1, String e2) {
/*  877 */     e.add(new Pair(Integer.valueOf(e1), e2));
   }




   public List<Pair<Integer, MapleCharacter>> newPair_chr() {
/*  884 */     return new ArrayList<>();
   }






   public void addToPair_chr(List<Pair<Integer, MapleCharacter>> e, int e1, MapleCharacter e2) {
/*  893 */     e.add(new Pair(Integer.valueOf(e1), e2));
   }




   public void broadcastPacket(byte[] packet) {
/*  900 */     if (this.disposed) {
       return;
     }
/*  903 */     for (MapleCharacter chr : getPlayers()) {
/*  904 */       chr.send(packet);
     }
   }





   public void broadcastTeamPacket(byte[] packet, int team) {
/*  913 */     if (this.disposed) {
       return;
     }
/*  916 */     for (MapleCharacter chr : getPlayers()) {
/*  917 */       if (chr.getTeam() == team) {
/*  918 */         chr.send(packet);
       }
     }
   }

   public void environmentChange(String env, int mode) {
/*  924 */     if (this.disposed) {
       return;
     }
/*  927 */     for (MapleCharacter chr : getPlayers()) {
/*  928 */       chr.send(MaplePacketCreator.environmentChange(env, mode, 0));
     }
   }

   public final void addInstanceMap(int mapid) {
/*  933 */     if (this.disposed) {
       return;
     }
/*  936 */     this.mapIds.add(Integer.valueOf(mapid));
/*  937 */     this.isInstanced.add(Boolean.valueOf(true));
   }















   public MapleMap createInstanceMap(int mapid) {
/*  955 */     if (this.disposed) {
/*  956 */       return null;
     }
/*  958 */     int assignedid = EventScriptManager.getNewInstanceMapId();
/*  959 */     this.mapIds.add(Integer.valueOf(assignedid));
/*  960 */     this.isInstanced.add(Boolean.valueOf(true));
/*  961 */     return getMapFactory().CreateInstanceMap(mapid, true, true, true, assignedid);
   }















   public MapleMap createInstanceMapS(int mapid) {
/*  979 */     if (this.disposed) {
/*  980 */       return null;
     }
/*  982 */     int assignedid = EventScriptManager.getNewInstanceMapId();
/*  983 */     this.mapIds.add(Integer.valueOf(assignedid));
/*  984 */     this.isInstanced.add(Boolean.valueOf(true));
/*  985 */     return getMapFactory().CreateInstanceMap(mapid, false, false, false, assignedid);
   }











   public MapleMap setInstanceMap(int mapid) {
/*  999 */     if (this.disposed) {
/* 1000 */       return getMapFactory().getMap(mapid);
     }
/* 1002 */     this.mapIds.add(Integer.valueOf(mapid));
/* 1003 */     this.isInstanced.add(Boolean.valueOf(false));
/* 1004 */     return getMapFactory().getMap(mapid);
   }




   public MapleMapFactory getMapFactory() {
/* 1011 */     return getChannelServer().getMapFactory();
   }

   public MapleMap getMapFactoryMap(int mapid) {
/* 1015 */     return getMapFactory().getMap(mapid);
   }





   public MapleMap getMapInstance(int args) {
/* 1023 */     if (this.disposed)
/* 1024 */       return null;  try {
       int trueMapID;
       MapleMap map;
/* 1027 */       boolean instanced = false;

/* 1029 */       if (args >= this.mapIds.size()) {
/* 1030 */         trueMapID = args;
       } else {
/* 1032 */         trueMapID = ((Integer)this.mapIds.get(args)).intValue();
/* 1033 */         instanced = ((Boolean)this.isInstanced.get(args)).booleanValue();
       }

/* 1036 */       if (!instanced) {
/* 1037 */         map = getMapFactory().getMap(trueMapID);
/* 1038 */         if (map == null) {
/* 1039 */           return null;
         }
/* 1041 */         if (map.getCharactersSize() == 0 && 
/* 1042 */           this.em.getProperty("shuffleReactors") != null && this.em.getProperty("shuffleReactors").equals("true")) {
/* 1043 */           map.shuffleReactors();
         }
       } else {

/* 1047 */         map = getMapFactory().getInstanceMap(trueMapID);
/* 1048 */         if (map == null) {
/* 1049 */           return null;
         }
/* 1051 */         if (map.getCharactersSize() == 0 && 
/* 1052 */           this.em.getProperty("shuffleReactors") != null && this.em.getProperty("shuffleReactors").equals("true")) {
/* 1053 */           map.shuffleReactors();
         }
       }

/* 1057 */       return map;
/* 1058 */     } catch (NullPointerException ex) {
/* 1059 */       log.error(ex);
/* 1060 */       return null;
     }
   }





   public void schedule(String methodName, long delay) {
/* 1069 */     if (this.disposed) {
       return;
     }
/* 1072 */     Timer.EventTimer.getInstance().schedule(() -> {
           if (this.disposed || this.em == null) {
             return;
           }
           try {
             this.em.getIv().invokeFunction(methodName, new Object[] { this });
/* 1078 */           } catch (ScriptException ex) {
             log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : " + methodName + ":\n", ex);
/* 1080 */           } catch (NoSuchMethodException noSuchMethodException) {}
         },delay);
   }








   public void schedule(String methodName, long delay, MapleCharacter player) {
/* 1092 */     if (this.disposed) {
       return;
     }
/* 1095 */     Timer.EventTimer.getInstance().schedule(() -> {
           if (this.disposed || this.em == null) {
             return;
           }
           try {
             this.em.getIv().invokeFunction(methodName, new Object[] { this, player });
/* 1101 */           } catch (ScriptException ex) {
             log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : " + methodName + ":\n" + ex);
/* 1103 */           } catch (NoSuchMethodException noSuchMethodException) {}
         },delay);
   }






   public String getName() {
/* 1113 */     return this.name;
   }





   public void setProperty(String key, String value) {
/* 1121 */     if (this.disposed) {
       return;
     }
/* 1124 */     this.props.setProperty(key, value);
   }







   public Object setProperty(String key, String value, boolean prev) {
/* 1134 */     if (this.disposed) {
/* 1135 */       return null;
     }
/* 1137 */     return this.props.setProperty(key, value);
   }





   public String getProperty(String key) {
/* 1145 */     if (this.disposed) {
/* 1146 */       return "";
     }
/* 1148 */     return this.props.getProperty(key);
   }




   public Properties getProperties() {
/* 1155 */     return this.props;
   }

   public final void setObjectProperty(Object obj1, Object obj2) {
/* 1159 */     if (this.disposed) {
       return;
     }
/* 1162 */     this.props.put(obj1, obj2);
   }

   public final Object getObjectProperty(Object obj) {
/* 1166 */     if (this.disposed) {
/* 1167 */       return null;
     }
/* 1169 */     return this.props.get(obj);
   }









   public void leftParty(MapleCharacter chr) {
/* 1181 */     if (this.disposed) {
       return;
     }
     try {
/* 1185 */       this.em.getIv().invokeFunction("leftParty", new Object[] { this, chr });
/* 1186 */     } catch (Exception ex) {
/* 1187 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : leftParty:\n", ex);
     }
   }






   public void disbandParty() {
/* 1197 */     if (this.disposed) {
       return;
     }
     try {
/* 1201 */       this.em.getIv().invokeFunction("disbandParty", new Object[] { this });
/* 1202 */     } catch (Exception ex) {
/* 1203 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : disbandParty:\n", ex);
     }
   }




   public void finishPQ() {
/* 1211 */     if (this.disposed) {
       return;
     }
     try {
/* 1215 */       this.em.getIv().invokeFunction("clearPQ", new Object[] { this });
/* 1216 */     } catch (NoSuchMethodException ex) {
/* 1217 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : clearPQ:\n", ex);
/* 1218 */     } catch (ScriptException scriptException) {}
   }











   public void removePlayer(MapleCharacter chr) {
/* 1232 */     if (this.disposed) {
       return;
     }
     try {
/* 1236 */       this.em.getIv().invokeFunction("playerExit", new Object[] { this, chr });
/* 1237 */     } catch (NoSuchMethodException ex) {
/* 1238 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : playerExit:\n", ex);
/* 1239 */     } catch (ScriptException scriptException) {}
   }






   public void onMapLoad(MapleCharacter chr) {
/* 1248 */     if (this.disposed) {
       return;
     }
     try {
/* 1252 */       this.em.getIv().invokeFunction("onMapLoad", new Object[] { this, chr });
/* 1253 */     } catch (ScriptException ex) {
/* 1254 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : onMapLoad:\n", ex);
/* 1255 */     } catch (NoSuchMethodException noSuchMethodException) {}
   }









   public void pickUpItem(MapleCharacter player, int itemID) {
/* 1267 */     if (this.disposed) {
       return;
     }
     try {
/* 1271 */       this.em.getIv().invokeFunction("pickUpItem", new Object[] { this, player, Integer.valueOf(itemID) });
/* 1272 */     } catch (NoSuchMethodException ex) {
/* 1273 */       log.error("Event name: " + this.em.getName() + ".js, Instance name : " + this.name + ", method Name : pickUpItem:\n", ex);
/* 1274 */     } catch (ScriptException scriptException) {}
   }







   public boolean isLeader(MapleCharacter chr) {
/* 1284 */     return (chr != null && chr.getParty() != null && chr.getParty().getLeader().getId() == chr.getId());
   }











   public void registerSquad(MapleSquad squad, MapleMap map, int questID) {
/* 1298 */     if (this.disposed) {
       return;
     }
/* 1301 */     int mapid = map.getId();
/* 1302 */     for (String chr : squad.getMembers()) {
/* 1303 */       MapleCharacter player = squad.getChar(chr);
/* 1304 */       if (player != null && player.getMapId() == mapid) {
/* 1305 */         if (questID > 0) {
/* 1306 */           player.getQuestNAdd(MapleQuest.getInstance(questID)).setCustomData(String.valueOf(System.currentTimeMillis()));
         }
/* 1308 */         registerPlayer(player);
/* 1309 */         if (player.getParty() != null) {
/* 1310 */           PartySearch ps = WorldPartyService.getInstance().getSearch(player.getParty());
/* 1311 */           if (ps != null) {
/* 1312 */             WorldPartyService.getInstance().removeSearch(ps, "开始组队任务，组队广告已被删除。");
           }
         }
       }
     }
/* 1317 */     squad.setStatus((byte)2);
/* 1318 */     squad.getBeginMap().broadcastMessage(MaplePacketCreator.stopClock());
   }











   public void registerSquad(MapleSquad squad, MapleMap map, String bossid) {
/* 1332 */     if (this.disposed) {
       return;
     }
/* 1335 */     int mapid = map.getId();
/* 1336 */     for (String chr : squad.getMembers()) {
/* 1337 */       MapleCharacter player = squad.getChar(chr);
/* 1338 */       if (player != null && player.getMapId() == mapid) {
/* 1339 */         if (bossid != null) {
/* 1340 */           player.setBossLog(bossid);
         }
/* 1342 */         registerPlayer(player);
/* 1343 */         if (player.getParty() != null) {
/* 1344 */           PartySearch ps = WorldPartyService.getInstance().getSearch(player.getParty());
/* 1345 */           if (ps != null) {
/* 1346 */             WorldPartyService.getInstance().removeSearch(ps, "开始组队任务，组队广告已被删除。");
           }
         }
       }
     }
/* 1351 */     squad.setStatus((byte)2);
/* 1352 */     squad.getBeginMap().broadcastMessage(MaplePacketCreator.stopClock());
   }










   public boolean isDisconnected(MapleCharacter chr) {
/* 1365 */     return (!this.disposed && this.dced.contains(Integer.valueOf(chr.getId())));
   }









   public void removeDisconnected(int id) {
/* 1377 */     if (this.disposed) {
       return;
     }
/* 1380 */     if (this.dced.contains(Integer.valueOf(id))) {
/* 1381 */       this.dced.remove(id);
     }
   }




   public EventManager getEventManager() {
/* 1389 */     return this.em;
   }





   public void applyBuff(MapleCharacter chr, int id) {
/* 1397 */     MapleItemInformationProvider.getInstance().getItemEffect(id).applyTo(chr);
/* 1398 */     chr.send(UIPacket.getStatusMsg(id));
   }





   public void applySkill(MapleCharacter chr, int id) {
/* 1406 */     SkillFactory.getSkill(id).getEffect(1).applyTo(chr);
   }








   public void EventGainNX() {
/* 1417 */     if (this.disposed) {
       return;
     }
/* 1420 */     int averlevel = getAverlevel();
/* 1421 */     for (MapleCharacter player : getPlayers()) {
/* 1422 */       player.modifyCSPoints(1, averlevel / 250 * 1000, true);
     }
   }

   public int getAverlevel() {
/* 1427 */     int ret = 0;
/* 1428 */     for (MapleCharacter player : getPlayers()) {
/* 1429 */       ret += player.getLevel();
     }
/* 1431 */     return ret / getPlayers().size();
   }

   public Map<String, String> getInfoStats() {
/* 1435 */     return this.infos;
   }

   public void setInfoStats(Map<String, String> infos) {
/* 1439 */     this.infos.clear();
/* 1440 */     this.infos.putAll(infos);
   }

   public void setPQLog(String log) {
/* 1444 */     getPlayers().parallelStream().forEach(p -> p.setPQLog(log));
   }

   public void setEventCount(String log) {
/* 1448 */     getPlayers().parallelStream().forEach(p -> p.setEventCount(log));
   }

   public void sendMarriedDone() {
/* 1452 */     broadcastPacket(MaplePacketCreator.sendMarriedDone());
   }

   public void showEffect(String effect) {
/* 1456 */     getPlayers().parallelStream().forEach(p -> p.getClient().announce(MaplePacketCreator.showEffect(effect)));
   }

   public void updateInfoQuest(int questid, String data) {
/* 1460 */     getPlayers().parallelStream().forEach(p -> p.updateInfoQuest(questid, data));
   }

   public void updateOneInfo(int questid, String key, String info) {
/* 1464 */     getPlayers().parallelStream().forEach(p -> p.updateOneInfo(questid, key, info));
   }

   public void startMapEffect(String message, int itemid, int effectType, boolean active) {
/* 1468 */     WorldBroadcastService.getInstance().broadcastMessage(MaplePacketCreator.startMapEffect(message, itemid, effectType, active));
   }

   public void broadcastWeatherEffectNotice(String msg, int type, int time) {
/* 1472 */     broadcastPacket(UIPacket.getMapEffectMsg(msg, type, time));
   }

   public void broadcastScriptProgressMessage(String s) {
/* 1476 */     broadcastPacket(UIPacket.getTopMsg(s));
   }

   public void setMonsterEscortInfo(MapleMonster mob, MapleCharacter player) {
/* 1480 */     if (mob != null) {
/* 1481 */       mob.switchController(player, false);
/* 1482 */       player.getClient().announce(MobPacket.mobEscortReturnBefore(mob, player.getMap()));
     }
   }

   public void updateOneQuestInfo(int n, String s, String s2) {
/* 1487 */     getPlayers().forEach(player -> player.updateOneQuestInfo(n, s, s2));
   }



   public int getAllItemNumber(int itemid) {
/* 1493 */     if (this.disposed) {
/* 1494 */       return 0;
     }
/* 1496 */     int quantity = 0;
/* 1497 */     for (MapleCharacter player : getPlayers()) {
/* 1498 */       quantity += player.getItemQuantity(itemid);
     }
/* 1500 */     return quantity;
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\event\EventInstanceManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */